package com.xc.ezplan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.ezplan.admin.entity.AdminQiniuConfig;
import com.xc.ezplan.admin.service.AdminQiniuConfigService;
import com.xc.ezplan.admin.mapper.AdminQiniuConfigMapper;
import com.xc.ezplan.admin.vo.AdminQiniuConfigVo;
import com.xc.ezplan.cache.constants.RedisKey;
import com.xc.ezplan.cache.utils.CacheUtil;
import com.xc.ezplan.core.exception.BadRequestException;
import com.xc.ezplan.core.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lixingchen
 * @date 2022/11/21
 * @description 七牛云配置实现类
 */
@Service
@Primary
@RequiredArgsConstructor
@CacheConfig(cacheNames = "aliConfig")
public class AdminQiNiuConfigServiceImpl extends ServiceImpl<AdminQiniuConfigMapper, AdminQiniuConfig>
		implements AdminQiniuConfigService {

	private final AdminQiniuConfigMapper adminQiniuConfigMapper;

	@Override
	@Cacheable(key = "'id:' + #p0")
	public Object queryQiNiuConfig(Long id) {
		return adminQiniuConfigMapper.selectById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateQiNiuConfig(AdminQiniuConfigVo adminQiniuConfigVo) {
		if (adminQiniuConfigVo.getId() == null) {
			throw new BadRequestException("配置的id不能为空");
		}
		String http = "http://", https = "https://";
		if (!(adminQiniuConfigVo.getHost().toLowerCase().startsWith(http)
				|| adminQiniuConfigVo.getHost().toLowerCase().startsWith(https))) {
			throw new BadRequestException("外链域名必须以http://或者https://开头");
		}
		AdminQiniuConfig adminQiniuConfig = new AdminQiniuConfig();
		BeanUtils.copyProperties(adminQiniuConfigVo, adminQiniuConfig);
		adminQiniuConfigMapper.update(adminQiniuConfig,
				new UpdateWrapper<AdminQiniuConfig>().lambda().eq(AdminQiniuConfig::getId, adminQiniuConfig.getId()));
		CacheUtil.deleteCache(RedisKey.QINIU_CONFIG_ID,adminQiniuConfigVo.getId());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void createQiNiuConfig(AdminQiniuConfigVo adminQiniuConfigVo) {
		AdminQiniuConfig adminQiniuConfig = new AdminQiniuConfig();
		BeanUtils.copyProperties(adminQiniuConfigVo, adminQiniuConfig);
		adminQiniuConfigMapper.insert(adminQiniuConfig);
	}

	@Override
	public Object queryAllQiNiuConfig(String keyword) {
		LambdaQueryWrapper<AdminQiniuConfig> queryWrapper = new QueryWrapper<AdminQiniuConfig>().lambda().like(
				StringUtils.isNotBlank(keyword), AdminQiniuConfig::getBucket,
				keyword);
		return adminQiniuConfigMapper.selectList(queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteQiNiuConfig(List<Long> ids) {
		adminQiniuConfigMapper.deleteBatchIds(ids);
		CacheUtil.deleteCache(RedisKey.QINIU_CONFIG_ID,ids);
	}

}
